<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Work extends CI_Controller {
// 
//  index.php
//  phpweb
//  
//  Created by WillLee on 2011-06-28.
//  Copyright 2011 lishuzu. All rights reserved.
// 

	function __construct()
	{
		parent::__construct();
		$this->load->model('work_model');
		$this->load->model('user_model');
		$this->load->model('tag_model');
		$this->load->model('feed_model');
		$this->load->model('collection_model');
 		$this->load->model('common/security_model');
		if($this->security_model->isLogin() === FALSE){
			showmessage('请登录', 'login', 2);
		}
		//关系模块
		$this->load->model('ship_model');
		
		$this->user = $this->session->userdata('user') ? $this->session->userdata('user') : '';
	}
	
	public function index($wid)
	{
		if (!$wid) {
			showmessage('您访问的作品不存在', 'index', 2);
		}
		$wid = intval($wid);
		
		$data['work'] = $this->work_model->get_work('wid', $wid);
		$data['islike'] = '0';
		if ($this->user) {
			$mycollection = $this->collection_model->get_collections_by_wid($wid,$this->user['uid']);//debug
			if (!empty($mycollection)) {
				$data['islike'] = '1';
			}
		}
		$data['userinfo'] = $this->user_model->getuser('uid', $data['work']['uid']);
		
		$data['self'] = $this->security_model->checkself($data['work']['uid']);

		if (!$data['self']) {
			$data['shipcode'] = $this->ship_model->get_ship($data['work']['uid']);
			$data['shipword'] = $this->ship_model->get_ship_word($data['shipcode']);
			//增加该作品浏览量
			$this->db->query("UPDATE ".$this->db->dbprefix('works')." SET viewnum = viewnum + 1 WHERE wid='".$wid."'");
		}
		//附加效果图
		$data['workpic'] = $this->work_model->getworkpic($wid);
		
		$this->load->template('work_tpl',$data);
	}
	
	public function edit($wid)
	{
		if (!$wid) {
			showmessage('您访问的作品不存在', 'index', 2);
		}
		$wid = intval($wid);
		
		$data['work'] = $this->work_model->get_work('wid', $wid);
		// 描述格式化
		$data['work']['description'] = str_replace("<br />", "\r\n", $data['work']['description']);
		// 标签格式化
		$data['work']['tags'] = implode(' ', $data['work']['tags']);
		
		$data['islike'] = '0';
		
		if (!checkperm('manageconfig')) {
			if ($this->user['uid'] != $data['work']['uid']) {
				showmessage('不是您的作品，不能编辑。', 'work/index/'.$wid);
			}
		}

		
		$data['userinfo'] = $this->user_model->getuser('uid', $this->user['uid']);
		
		$data['self'] = $this->security_model->checkself($this->user['uid']);

		
		if (!$data['self']) {
			$data['shipcode'] = $this->ship_model->get_ship($data['work']['uid']);
			$data['shipword'] = $this->ship_model->get_ship_word($data['shipcode']);
		}
		// 作品扩展信息
		$data['workfield'] = $this->work_model->get_workfield($wid);
		
		//附加效果图
		$data['workpic'] = $this->work_model->getworkpic($wid);
		
		if (submitcheck('worksubmit')) {
			$title = $this->input->post('title', true);
			$description = $this->input->post('description', true);
			$expectprice = $this->input->post('expectprice', true);
			$tag = $this->input->post('tag', true);
			if (!$this->user['username']) {
				showmessage('请先登录');
			}
			
			if (empty($title) || empty($description) || empty($expectprice)) {
				showmessage('信息未填写完整');
			}
			
			$updatedata = array(
				'wid'=> $wid,
				'title' => trim($title),
				'description' => $description,
				'expectprice' => $expectprice,
				'updatetime' => now(),
				);

			// 换行
			$updatedata['description'] = str_replace(array("\n", "\r\n"), "<br/>", $updatedata['description']);
			// 格式化描述
			$this->load->helper('bbcode');
			$updatedata['description'] = html2bbcode($updatedata['description']);
			//取消html
			$updatedata = shtmlspecialchars($updatedata);
			
			//TAG 此处不能进行html过滤 会把序列化里的分号转义
			$tag = getstr($tag, 500, 1, 1, 1);	//语词屏蔽
			$oldtagstr = empty($data['work']['tag'])?'':implode(' ', unserialize($data['work']['tag']));
			$tagarr = $this->tag_model->update_tag($tag, $data['work']['tag'], $wid);
			
			$updatedata['tag'] = empty($tagarr)?'':serialize($tagarr);
			
			// 上传设计图
			if ($_FILES['design']) {
				$this->work_model->upload_design($wid);
			}
			
			$res = $this->work_model->update($updatedata);
			showmessage('修改成功','work/index/'.$wid, 3);
		}
		
		$this->load->template('work_edit_tpl',$data);
	}
	
	public function remove($wid)
	{
		if (!$wid) {
			showmessage('您访问的作品不存在', 'index', 2);
		}
		$wid = intval($wid);
		
		$data['work'] = $this->work_model->get_work('wid', $wid);
		$data['islike'] = '0';
		if ($this->user['uid'] != $data['work']['uid']) {
			showmessage('不是您的作品，不能删除。', 'work/index/'.$wid, 2);
		}
		
		$data['userinfo'] = $this->user_model->getuser('uid', $this->user['uid']);
		
		$data['self'] = $this->security_model->checkself($this->user['uid']);
		if (!$data['self']) {
			$data['shipcode'] = $this->ship_model->get_ship($data['work']['uid']);
			$data['shipword'] = $this->ship_model->get_ship_word($data['shipcode']);
		}
		$res = $this->work_model->remove($wid);
		if ($res) {
			//减少作品数目
			if ($wid) {
			// 更新session
			$this->security_model->update_user_session();
			}
			showmessage('删除成功', 'worklist/index/'.$this->user['uid'], 2);
		}else{
			showmessage('删除失败，作品可能已经被推荐生产。', 'worklist/index/'.$this->user['uid'], 2);
		}
	}
	
	public function upwork(){
		if (submitcheck('uploadsubmit')) {
			$title = $this->input->post('title', true);
			$description = $this->input->post('description', true);
			$expectprice = $this->input->post('expectprice', true);
			if (!$this->user['username']) {
				showmessage('请先登录');
			}
			if (empty($title) || empty($description) || empty($expectprice)) {
				showmessage('信息未填写完整');
			}
			if (!$_FILES['workpic']['name']) {
				showmessage('未选择图片');
			}
			$insertdata = array(
				'title' => trim($title),
				'description' => $description,
				'expectprice' => $expectprice,
				'uid' => $this->user['uid'],
				'name' => $this->user['name'],
				'dateline' => now(),
				'updatetime' => now(),
				//默认审核通过
				'status' => '1',
				);
				
			// 换行
			$insertdata['description'] = str_replace(array("\n", "\r\n"), "<br/>", $insertdata['description']);
			
			// 格式化描述
			$this->load->helper('bbcode');
			$insertdata['description'] = html2bbcode($insertdata['description']);
			
			//取消html
			$insertdata = shtmlspecialchars($insertdata);
			
			//增加作品
			$wid = $this->work_model->add($insertdata);
			
			if ($wid) {
				// TAG 处理
				$tag = shtmlspecialchars(trim($this->input->post('tag', true)));
				$tag = getstr($tag, 500, 1, 1, 1);	//语词屏蔽
				$tagarr = $this->tag_model->tag_batch($wid, $tag);
				//更新附表中的tag
				$tagdata = empty($tagarr)?'':serialize($tagarr);
				$this->db->where('wid', $wid);
				$this->db->update('works', array('tag'=>$tagdata));

				//增加作品数目
				$this->db->query("UPDATE ".$this->db->dbprefix('user')." SET worknum = worknum + 1 WHERE uid='".$this->user['uid']."'");
				
				// 更新session
				$this->security_model->update_user_session();
			}
			
			// 上传文件
			if ($_FILES['workpic']['name']) {
				$res = $this->work_model->upload_workimg($wid);
			}
			
			// 发送feed
			$this->feed_model->feed_publish($wid, 'workid', 1);
			showmessage('操作成功','worklist/index', 3);
		}else{
			showmessage('操作失败','worklist/index', 3);
		}
	}
}

/* End of file index.php */
/* Location: ./application/controllers/index.php */